package vue;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

import controler.ActionE;
import controler.Controler;
import controler.LoadSaveManager;

public class MainIHM extends JFrame{

	private static final String APP_NAME = "TaskManager";
	private static final ImageIcon MAIN_ICON = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("data/mainIcon.png"));

	private static final long serialVersionUID = 1L;
	private Controler controler = null;
	
	
	public MainIHM(Controler controler) {
		super(APP_NAME + " " + LoadSaveManager.CURRENT_VERSION);
		setIconImage(MAIN_ICON.getImage());
		this.controler = controler;
		setBounds(50, 50, 1350, 950);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                    int confirmed = JOptionPane.showConfirmDialog(null,
                                    "Are you sure you want to exit " + APP_NAME + "?", "User Confirmation",
                                    JOptionPane.YES_NO_OPTION);
                    if (confirmed == JOptionPane.YES_OPTION) {
                    	MainIHM.this.controler.closeAllWatchers();
                    	exit(MainIHM.this);
                    }    
            }
		});
		
		setJMenuBar(buildMenuBar());
		getContentPane().add(buildJToolbar(), BorderLayout.NORTH);
		
		setVisible(true);
	}
	
    private static void exit(JFrame j) {
        j.dispose();
        System.exit(0);
    }
	
	private JToolBar buildJToolbar() {
		JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
		toolbar.setFloatable(false);
		//toolbar.setPreferredSize(new Dimension(toolbar.getPreferredSize().width, 30));
		//toolbar.add(ActionE.NEW_LIST.getActionButton(controler));
		//toolbar.addSeparator();
		//toolbar.add(new Box.Filler(new Dimension(20,20), new Dimension(Short.MAX_VALUE, 20), new Dimension(Short.MAX_VALUE, 20)));
		return toolbar;
	}

	private JMenuBar buildMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu fileMenu = new JMenu("File");
		fileMenu.addSeparator();
		fileMenu.add(ActionE.QUIT.getMenuItem(controler));
		menuBar.add(fileMenu);
		return menuBar;
	}
	
	public void updateView() {
		this.repaint();
	}
	
}
